import { View } from "@tarojs/components"
import { PureStandardProps } from "../consts"
import CouponModel from "../lib/models/coupon_model"

import "./coupon-item.scss"

interface CouponItemProps extends PureStandardProps {
    model: CouponModel
    onClick: (model: CouponModel) => void
}

const CouponItem = (props: CouponItemProps) => {

    const { model } = props

    let onClick = () => {
        props.onClick(model)
    }

    let statusClassName = "coupon-item-unget"
    if (model.isget) {
        statusClassName = "coupon-item-geted"
    } else if (Number.parseInt(model.needscore) > 0) {
        statusClassName = "coupon-item-score"
    }

    let descChildren = new Array()
    descChildren.push(<View>{ model.price }元优惠劵</View>)
    if (model.isget) {
        descChildren.push(<View>{ model.addtime }-{model.endtime}</View>)
    } else {
        descChildren.push(<View>领取后{ model.activedays }天有效</View>)
    }
    descChildren.push(<View>全部商品适用</View>)

    let getTitle
    if (model.isget) {
        if (Number.parseInt(model.needscore) > 0) {
            getTitle = "已兑换"
        } else {
            getTitle = "已领取"
        }
    } else {
        if (Number.parseInt(model.needscore) > 0) {
            getTitle = "兑换"
        } else {
            getTitle = "领取"
        }
    }

    return (
        <View {...props} className={["coupon-item", props.className, statusClassName].join(" ")} onClick={ onClick }>

            <View className="coupon-item-warp">
                <View className="coupon-item-price-warp">
                    <View className="coupon-item-price">￥{ model.price }</View>
                    <View className="coupon-item-price-desc">{ model.tip }</View>
                </View>

                <View className="coupon-item-desc">
                    { descChildren }
                </View>

                <View className="coupon-item-get-warp">
                    <View className="coupon-item-get-title">{ getTitle }</View>
                    <View className="coupon-item-get-desc">{ Number.parseInt(model.needscore) > 0 ? `消耗${model.needscore}积分` : "会员领取"}</View>
                </View>
            </View>
        </View>
    )
}

export default CouponItem